package com.jiangyg.mall.db;

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.jiangyg.mall.core.utils.springframework.ResourceUtils;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.core.io.Resource;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 类描述：Mybatis基础配置
 * 
 * @author jiangyg
 * @date 2020-07-07
 * @version 2.0
 */
public abstract class MyBatisBaseConfigurer {

    /**
     * 功能描述：构建MybatisSqlSessionFactoryBean
     *
     * @param dataSource      数据源
     * @param globalConfig    全局配置
     * @param mapperLocations 自动扫描mapping.xml文件映射路径
     * @return 返回MybatisSqlSessionFactoryBean
     * @throws IOException IO异常
     */
    protected MybatisSqlSessionFactoryBean buildSessionFactoryBean(DataSource dataSource, GlobalConfig globalConfig, List<String> mapperLocations) throws IOException {
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setGlobalConfig(globalConfig);
        // 设置mapper扫描路径
        Resource[] resources = ResourceUtils.obtainResourcesByPathMatching(mapperLocations);
        sqlSessionFactoryBean.setMapperLocations(resources);
        // 源码里面如果有configuration, 不会注入BaseMapper里面的方法, 所以这里要这样写
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setMapUnderscoreToCamelCase(true);
        sqlSessionFactoryBean.setConfiguration(configuration);
        // 设置分页插件
        List<Interceptor> interceptors = new ArrayList<>();
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        // 设置插件
        interceptors.add(paginationInterceptor);
        sqlSessionFactoryBean.setPlugins(interceptors.toArray(new Interceptor[0]));
        return sqlSessionFactoryBean;
    }

    /**
     * 功能描述：MapperScannerConfigurer 是 BeanFactoryPostProcessor 的一个实现， <br />
     * 如果配置类中出现BeanFactoryPostProcessor ，会破坏默认的 post-processing, 如果不加static,<br />
     * 会导致整个都提前加载,这时候, 取不到projectStage的值,<br />
     * 此处配置可以采用@MapperScan注解
     *
     * @param basePackage 自动扫描mapper映射路径
     * @return MapperScannerConfigurer
     */
    protected MapperScannerConfigurer buildMapperScannerConfigurer(String basePackage) {
        MapperScannerConfigurer configurer = new MapperScannerConfigurer();
        configurer.setBasePackage(basePackage);
        configurer.setAnnotationClass(Mapper.class);
        configurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        return configurer;
    }

}
